package com.qiqi.admin.basicdata.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qiqi.basicdata.dto.OffsetDTO;
import com.qiqi.basicdata.entity.OffsetDO;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiqi.common.entity.PageEntity;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import com.qiqi.basicdata.service.OffsetService;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author QiQiDream
 * @since 2020-11-28
 */
@Api("胶印机相关接口")
@RestController
@RequestMapping("/offset")
public class OffsetController {

    @Resource
    private OffsetService offsetService;

    @ApiOperation(value = "获取(列表)")
    @PostMapping("/list")
    public PageEntity<OffsetDO> getOffsetPage(@RequestBody OffsetDTO offset) {
        LambdaQueryWrapper<OffsetDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(offset.getCode()),OffsetDO::getCode,offset.getCode())
                .like(StringUtils.isNotBlank(offset.getName()),OffsetDO::getName,offset.getName());
        IPage<OffsetDO> iPage = offsetService.page(new Page<>(offset.getPage(),offset.getCount()),queryWrapper);
        return new PageEntity<>(iPage.getTotal(),Convert.convert(new TypeReference<List<OffsetDO>>() {}, iPage.getRecords()));
    }

    @ApiOperation(value = "获取(单个)")
    @GetMapping("/{id}")
    public OffsetDO getOffset(@PathVariable Long id) {
        return offsetService.getById(id);
    }

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public Boolean saveOffset(@RequestBody OffsetDO offsetDO) {
        return offsetService.saveOrUpdate(offsetDO);
    }

    @ApiOperation(value = "删除(单个))")
    @DeleteMapping("/{id}")
    public Boolean deleteOffsetById(@PathVariable Long id) {
        return offsetService.removeById(id);
    }
}
